﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Collections.Generic;
using System.IO;

using CE.User;
using CE.Global;
using CE.Database;
using CE.CarInsurance;

public partial class carinsurance_PremiumDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            if(Request.QueryString["carid"] != null)
            {
                string VehicleNo = "";
                Int64 CECarId = 0;
                Int64 RefCEUserId = 0;
                string[] vals = Request.QueryString["carid"].ToString().Split(',');
                if (vals.Length == 3)
                {
                    CECarId = Convert.ToInt64(vals[0].ToString());
                    RefCEUserId = Convert.ToInt64(vals[1].ToString());
                    VehicleNo = vals[2].ToString();
                }

                GeneratePremium(CECarId,RefCEUserId, VehicleNo);
            }
        }
    }

    public void GeneratePremium(Int64 CECarId, Int64 RefCEUserId, string VehicleNo)
    {

        Int32 _Count = 0;
        Label lblContent = new Label();
        string _html = "";
        string _fileName = "FileName";
        _fileName = "Premium_" + CECarId;
        string _UserCover = "";

        DataSet _dsPremium = new DataSet();
        BrokerPremiumDB _prokerpremiumdb = new BrokerPremiumDB();
        _dsPremium = _prokerpremiumdb.GetBrokerPremium(CECarId, RefCEUserId);
        if (_dsPremium.Tables[0].Rows.Count > 0)
        {
            if (_dsPremium.Tables.Count > 1)
            {
                if (_dsPremium.Tables[2].Rows.Count > 0)
                {
                    _UserCover = _dsPremium.Tables[2].Rows[0]["ProviderCoverTypeDesc"].ToString();
                }
            }

            _Count = _dsPremium.Tables[0].Rows.Count;
            if (_Count > 4)
            {
                _Count = 4;
            }

            #region variables
            string[] _Insurer = new string[_Count];
            string[] _Workshop = new string[_Count];
            string[] _Premium = new string[_Count];
            string[] _Excess = new string[_Count];
            string[] _QuoteId = new string[_Count];
            string[] _Cover = new string[_Count];
            string[] _Package = new string[_Count];
            #endregion

            for (int i = 0; i < _Count; i++)
            {
                #region Values assign to variables
                _Insurer[i] = _dsPremium.Tables[0].Rows[i]["CompanyName"].ToString();
                _Workshop[i] = _dsPremium.Tables[0].Rows[i]["WorkShop"].ToString();
                _Premium[i] = "$" + _dsPremium.Tables[0].Rows[i]["Premium"].ToString();
                _Excess[i] = "$" + _dsPremium.Tables[0].Rows[i]["Excess"].ToString();
                _QuoteId[i] = CECarId.ToString();
                _Cover[i] = _dsPremium.Tables[0].Rows[i]["ProviderCoverTypeDesc"].ToString();
                _Package[i] = _dsPremium.Tables[0].Rows[i]["Package"].ToString();
                #endregion
            }

            #region Html

            Uri myUri = new Uri(HttpContext.Current.Request.Url.ToString());
            string domainUrl = "";
            string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            if (_strCXLive == "2")
            {
                domainUrl = "http://" + myUri.Authority;
            }
            else
            {
                domainUrl = "http://" + myUri.Host;
            }
            string imgUrl = domainUrl + "/CX_CRM/images/header/kim_banner.png";

            _html = "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0'>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'><img src='" + imgUrl + "' alt='KIM Brokers' /></td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>&nbsp;</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>Below are the instant competitive quotations sourced from some insurers.</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>&nbsp;</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>Vehicle Number: " + VehicleNo + "</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>Type of Cover: " + _UserCover.ToString() + "</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>&nbsp;</td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'><b>QUOTES</b></td>"
                    + "</tr>"

                    + "<tr>"
                    + "<td align='left' style='width:100%'>"
                    + "<table style='width: 100%; background-color: white;' cellpadding='5' cellspacing='2' border='1'>"
                    + "<tr>"
                    + "<td align='left' style='width:150px'>&nbsp;</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'>Quotation " + j + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Quote ID</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'> " + _QuoteId[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Insurer</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'> " + _Insurer[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Restriction on Workshop</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'> " + _Workshop[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"
                
            + "<tr>"
            + "<td align='left' style='width:150px'>Cover Type</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'> " + _Cover[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Package</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px'> " + _Package[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Premium</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px; color:blue; font-weight:bold;'> " + _Premium[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"

            + "<tr>"
            + "<td align='left' style='width:150px'>Excess (Insured + Named Drivers)</td>";
            for (int j = 1; j <= _Count; j++)
            {
                _html += "<td align='center' style='width:250px; color:blue; font-weight:bold;'> " + _Excess[j - 1].ToString() + "</td>";
            }
            _html += "</tr>"
            + "</table>"
            + "</td>"
            + "</tr>"

            + "<tr>"
            + "<td align='left' style='width:100%'>&nbsp;</td>"
            + "</tr>"

            + "<tr>"
            + "<td align='left' style='width:100%'>&bull; This Quote Is Subject To the Insurer's Final Confirmation And Coverage Is Only Effective When The Insurer Has Accepted The Risk</td>"
            + "</tr>"

            + "<tr>"
            + "<td align='left' style='width:100%'>&bull; The Insurer Reserves The Right To Revise or Withdraw The Quote If Any Of the Information Given Differs From The Actual Risk</td>"
            + "</tr>"

            + "<tr>"
            + "<td align='left' style='width:100%'>&bull; Unless Otherwise Specified, The Quote Is Valid Only For 14 Days From The Date Of Quote</td>"
            + "</tr>"

            + "<tr>"
            + "<td align='left' style='width:100%'>&bull; All premiums quoted are nett, ie where applicable, inclusive of NCD, Safe Driver Discount and prevailing GST</td>"
            + "</tr>"

            + "</table>";
            #endregion

            string _HeaderText = "";
            lblPremiumHtml.Text = _html;
            ExportHtml(_fileName, lblPremiumHtml, _HeaderText);
        }
    }

    public void ExportHtml(string fileName, Label lblPremiumHtml, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".htm";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "text/HTML";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        lblPremiumHtml.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblPremiumHtml);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();

        //string attachment = "attachment; filename=" + fileName + ".xls";
        //Response.ClearContent();
        //Response.AddHeader("content-disposition", attachment);
        //Response.ContentType = "application/ms-excel";
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //// Create a form to contain the grid
        //HtmlForm frm = new HtmlForm();
        //frm.InnerHtml = headerText;
        //lblPremiumHtml.Parent.Controls.Add(frm);
        //frm.Attributes["runat"] = "server";
        //frm.Controls.Add(lblPremiumHtml);
        //frm.RenderControl(htw);
        //Response.Write(sw.ToString());
        //Response.End();
    }

}
